<?php
/**
 * Item form definition 
 *
 * @author guzhi
 * @since 2009-04-01
 */
 
 require_once BaseUtil::getModulePath("base")  .  '/forms/BaseForm.php';
 require_once   BaseUtil::getModulePath("base")  .  '/validate/Unique.php';

 class ItemForm extends BaseForm{
 	public function __construct($item = array()){
 		parent::__construct();
 		
 		$this->setAction(HttpUtil::getUrl("product", 'saveitem'));
 		
 		$id = new Zend_Form_Element_Hidden('id');
 		if(isset($item['id'])){
			$id->setValue($item['id']);
			$validId = $item['id'];
		} else {
			$validId = null;
		}
 		
 		$attribute1 = new Zend_Form_Element_Text('attribute1');
 		$attribute1->setLabel($this->translate->_('attribute') . " 1");
 		$attribute1->addFilter("StringTrim");
 		if(isset($item['attribute_1'])){
			$attribute1->setValue($item['attribute_1']);
		}
 		$value1 = new Zend_Form_Element_Text('value1');
 		$value1->setLabel($this->translate->_('value') . " 1");
 		$value1->addFilter("StringTrim");
 		if(isset($item['value_1'])){
			$value1->setValue($item['value_1']);
		}
 		
 		$attribute2 = new Zend_Form_Element_Text('attribute2');
 		$attribute2->setLabel($this->translate->_('attribute') . " 2");
 		$attribute2->addFilter("StringTrim");
 		if(isset($item['attribute_2'])){
			$attribute2->setValue($item['attribute_2']);
		}
 		$value2 = new Zend_Form_Element_Text('value2');
 		$value2->setLabel($this->translate->_('value') . " 2");
 		$value2->addFilter("StringTrim");
 		if(isset($item['value_2'])){
			$value2->setValue($item['value_2']);
		}
 		
 		$attribute3 = new Zend_Form_Element_Text('attribute3');
 		$attribute3->setLabel($this->translate->_('attribute') . " 3");
 		$attribute3->addFilter("StringTrim");
 		if(isset($item['attribute_3'])){
			$attribute3->setValue($item['attribute_3']);
		}
 		$value3 = new Zend_Form_Element_Text('value3');
 		$value3->setLabel($this->translate->_('value') . " 3");
 		$value3->addFilter("StringTrim");
 		if(isset($item['value_3'])){
			$value3->setValue($item['value_3']);
		}
 		
 		$attribute4 = new Zend_Form_Element_Text('attribute4');
 		$attribute4->setLabel($this->translate->_('attribute') . " 4");
 		$attribute4->addFilter("StringTrim");
 		if(isset($item['attribute_4'])){
			$attribute4->setValue($item['attribute_4']);
		}
 		$value4 = new Zend_Form_Element_Text('value4');
 		$value4->setLabel($this->translate->_('value') . " 4");
 		$value4->addFilter("StringTrim");
 		if(isset($item['value_4'])){
			$value4->setValue($item['value_4']);
		}
 		
 		$attribute5 = new Zend_Form_Element_Text('attribute5');
 		$attribute5->setLabel($this->translate->_('attribute') . " 5");
 		$attribute5->addFilter("StringTrim");
 		if(isset($item['attribute_5'])){
			$attribute5->setValue($item['attribute_5']);
		}
 		$value5 = new Zend_Form_Element_Text('value5');
 		$value5->setLabel($this->translate->_('value') . " 5");
 		$value5->addFilter("StringTrim");
 		if(isset($item['value_5'])){
			$value5->setValue($item['value_5']);
		}
 		
 		$upc = new Zend_Form_Element_Text('upc');
 		$upc->setLabel($this->translate->_('upc'));
 		$upc->addFilter("StringTrim");
 		$validUniqueUpc = new MyValid_Unique();
		$validUniqueUpc->setFetchAttr('product_items', 'upc', $validId);
        $upc->addValidator($validUniqueUpc);
 		if(isset($item['upc'])){
			$upc->setValue($item['upc']);
		}
		
 		$sku = new Zend_Form_Element_Text('sku');
 		$sku->setLabel($this->translate->_('sku'));
 		$sku->addFilter("StringTrim");
 		$validUniqueSku = new MyValid_Unique();
		$validUniqueSku->setFetchAttr('product_items', 'sku', $validId);
        $sku->addValidator($validUniqueSku);
 		if(isset($item['sku'])){
			$sku->setValue($item['sku']);
		}
 		
 		$purchasingPrice = new Zend_Form_Element_Text('purchasingPrice');
 		$purchasingPrice->setLabel($this->translate->_('purchasingPrice'));
 		$purchasingPrice->addFilter("StringTrim");
 		$purchasingPrice->setRequired(true);
 		if(isset($item['purchasing_price'])){
			$purchasingPrice->setValue($item['purchasing_price']);
		}
		
 		$costPrice = new Zend_Form_Element_Text('costPrice');
 		$costPrice->setLabel($this->translate->_('costPrice'));
 		$costPrice->addFilter("StringTrim");
 		$costPrice->setRequired(true);
 		if(isset($item['cost_price'])){
			$costPrice->setValue($item['cost_price']);
		}
 		
 		$listPrice = new Zend_Form_Element_Text('listPrice');
 		$listPrice->setLabel($this->translate->_('listPrice'));
 		$listPrice->addFilter("StringTrim");
 		$listPrice->setRequired(true);
 		if(isset($item['list_price'])){
			$listPrice->setValue($item['list_price']);
		}
 		
 		$weight = new Zend_Form_Element_Text('weight');
 		$weight->setLabel($this->translate->_('weight'));
 		$weight->addFilter("StringTrim");
 		if(isset($item['weight'])){
			$weight->setValue($item['weight']);
		}
		
 		$inventory = new Zend_Form_Element_Text('inventory');
 		$inventory->setLabel($this->translate->_('inventory'));
 		$inventory->addFilter("Int");
 		if(isset($item['inventory'])){
			$inventory->setValue($item['inventory']);
		}
		
 		$description = new Zend_Form_Element_Textarea('description');
 		$description->setLabel($this->translate->_('description'));
 		$description->setAttribs(array("rows" => 5, "cols" => 40));
 		$description->addFilter("StringTrim");
 		if(isset($item['description'])){
			$description->setValue($item['description']);
		}
 		
 		$productId = new Zend_Form_Element_Hidden('productId');
 		if(isset($item['product_id'])){
 			$productId->setValue($item['product_id']);
 		}
 		
		
 		$this->addElements(array(
 				$id, 
 				$attribute1, $value1, 
 				$attribute2, $value2, 
 				$attribute3, $value3, 
 				$attribute4, $value4, 
 				$attribute5, $value5, 
 				$upc, 
 				$sku, 
 				$purchasingPrice, 
 				$costPrice,
 				$listPrice, 
 				$weight, 
 				$inventory, 
 				$description, 
 				$productId
 			));
		$this->setElementDecorators(array(
            'ViewHelper',
            array(array('div' => 'HtmlTag'), array('tag' => 'div',"class" => "form_element")),
        ));
 	}
 }
 
 ?>